package my.leetcode.solution;

/**
 * 思路:
 */
public class P41 {

    public static void main(String[] args) {
        System.out.println(firstMissingPositive(new int[] {3,4,-1,1}));
        System.out.println(firstMissingPositive(new int[] {1}));
    }


    public static int firstMissingPositive(int[] nums) {
        int len = nums.length;
        for(int i = 0;i < len;i++) {
            int n = nums[i],now = i,index = n - 1;
            while (index != now && index < len && index > -1) {
                int t = nums[index];
                nums[index] = n;
                n = t;
                now = index;
                index = n - 1;
            }
        }
        int i = 0;
        for(;i < len;i++) {
            if(i != nums[i] - 1) {
                return i + 1;
            }
        }
        return i + 1;
    }

}
